package edu.whu.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Calendar;

/**
 * @author 示例Aspect
 */

@Aspect
@Component
public class DemoAspect {

    @Pointcut("execution(* edu.whu.service.*.*(..))")
    public void servicePointCut(){
    }

    @Before("servicePointCut()")
    public void beforePointCut(JoinPoint jp){
        String info= "beforePointCut:进入"+jp.getSignature()+"方法,";
        info +="参数是：";
        for (Object arg : jp.getArgs()) {
            info+= arg+" ";
        }
        System.out.println(info);
    }

    @After("servicePointCut()")
    public void afterPointCut(JoinPoint jp) {
        System.out.println("afterPointCut:退出方法...");
    }


    @Around("servicePointCut()")
    public Object aroundPointcut(ProceedingJoinPoint joinPoint) throws Throwable {
        long t1= Calendar.getInstance().getTimeInMillis();
        System.out.println("aroundPointcut:进入"+joinPoint.getSignature()+"方法...");
        Object retVal = joinPoint.proceed();
        long t2= Calendar.getInstance().getTimeInMillis();
        System.out.println("aroundPointcut:退出"+joinPoint.getSignature()+"方法,耗时："+(t2-t1));
        return retVal;
    }

    @AfterReturning(pointcut= "servicePointCut()", returning = "value")
    public void afterPointCutReturn(JoinPoint jp,String value) {
        System.out.println("afterPointCutReturn:返回值为："+value);
    }


    @AfterThrowing(pointcut= "servicePointCut()",throwing="ex")
    public void afterPointCutThrow(JoinPoint jp,Exception ex) {
        System.out.println("afterPointCutThrow:异常为："+ex);
    }







}
